import 'package:flutter/material.dart';

import 'LoginContractView.dart';
import 'LoginModel.dart';
import 'LoginPresenter.dart';

class LoginScreen extends StatefulWidget {
  @override
  _LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen>
    implements LoginContractView {

  late LoginPresenter _presenter;
  String _message = '';

  @override
  void initState() {
    super.initState();
    _presenter = LoginPresenter(this, LoginModel());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('MVP 示例')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            TextField(
              decoration: InputDecoration(labelText: '用户名'),
              onChanged: (value) => _username = value,
            ),
            TextField(
              decoration: InputDecoration(labelText: '密码'),
              obscureText: true,
              onChanged: (value) => _password = value,
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                _presenter.login(_username, _password);
              },
              child: Text('登录'),
            ),
            SizedBox(height: 20),
            Text(_message),
          ],
        ),
      ),
    );
  }

  String _username = '';
  String _password = '';

  @override
  void onLoginSuccess(String message) {
    setState(() {
      _message = message;
    });
  }

  @override
  void onLoginError(String error) {
    setState(() {
      _message = error;
    });
  }
}
